package cn.kwq.project_manger.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.kwq.project_manger.aop.annotations.LimitRequest;
import cn.kwq.project_manger.common.R;
import cn.kwq.project_manger.common.RES;
import cn.kwq.project_manger.exception.FullTeamException;
import cn.kwq.project_manger.exception.UnBindWyuSystemException;
import cn.kwq.project_manger.pojo.User;
import cn.kwq.project_manger.pojo.dto.AddTeamDto;
import cn.kwq.project_manger.pojo.dto.BindProjTeamDto;
import cn.kwq.project_manger.pojo.dto.FindTeamDto;
import cn.kwq.project_manger.pojo.dto.LoginDto;
import cn.kwq.project_manger.repostitory.TeamRepository;
import cn.kwq.project_manger.service.JoinTeamService;
import cn.kwq.project_manger.service.LoginService;
import cn.kwq.project_manger.service.TeamProjectService;
import cn.kwq.project_manger.service.TeamService;
import com.alibaba.fastjson2.JSONObject;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Slf4j
@Api(tags ="队伍服务")
@RestController
@RequestMapping("/team/")
public class TeamController {

    @Autowired
    TeamService teamService;

    @Autowired
    JoinTeamService joinTeamService;

    @Autowired
    TeamProjectService teamProjectService;

    @Autowired
    TeamRepository teamRepository;


    @ApiOperation("新建队伍")
    @SaCheckLogin//
    @PostMapping("addTeam")
    public RES addTeam(@RequestBody AddTeamDto dto) throws UnBindWyuSystemException {
       return R.ok("创建成功",teamService.AddNewTeam(dto,StpUtil.getLoginIdAsLong()));
    }


    @ApiOperation("通过入队申请")
    @SaCheckLogin//
    @PostMapping("accept2JoinTeam")
    public RES accept2JoinTeam(@ApiParam(name = "teamId和joinUserId", value = "队伍的id，要加入的队员id", required = true)
                               @RequestBody Map<String,Long> object) throws UnBindWyuSystemException, FullTeamException {
        Long teamId=  object.get("teamId");
        Long joinUserId=object.get("joinUserId");
        if (!teamService.isCaptain(StpUtil.getLoginIdAsLong(),teamId)){
            throw new RuntimeException("队伍队长才有权操作");
        }
        if (joinTeamService.isFull(teamId)){
            throw new FullTeamException("队伍已满员");
        }
        if (teamId.equals(joinUserId)){
            throw new RuntimeException("队长已在队伍中");
        }
        return R.ok("已通过",teamService.acceptJoin(StpUtil.getLoginIdAsLong(),teamId,joinUserId));
    }


    @ApiOperation("检查入队申请是否通过")
    @SaCheckLogin//
    @PostMapping("checkJoin")
    public RES checkJoin(@ApiParam(name = "teamId和joinUserId", value = "队伍的id，要加入的队员id", required = true)
                               @RequestBody Map<String,Long> object) throws UnBindWyuSystemException, FullTeamException {
        Long teamId=  object.get("teamId");
        Long joinUserId=object.get("joinUserId");
        return R.ok(teamService.isCaptionCheck(teamId,joinUserId));
    }


    @ApiOperation("拒绝入队申请")
    @SaCheckLogin//
    @PostMapping("reject2JoinTeam")
    public RES reject2JoinTeam(@ApiParam(name = "teamId和joinUserId", value = "队伍的id，要加入的队员id", required = true)
                               @RequestBody Map<String,Long> object) throws UnBindWyuSystemException, FullTeamException {
        Long teamId=  object.get("teamId");
        Long joinUserId=object.get("joinUserId");
        if (!teamService.isCaptain(StpUtil.getLoginIdAsLong(),teamId)){
            throw new RuntimeException("队伍队长才有权操作");
        }
        if (teamId.equals(joinUserId)){
            throw new RuntimeException("队长已在队伍中,不可操作");
        }
        return R.ok(teamService.rejectJoin(StpUtil.getLoginIdAsLong(),teamId,joinUserId));
    }

    @ApiOperation("申请入队")
    @SaCheckLogin//
    @PostMapping("apply2JoinTeam")
    public RES apply2JoinTeam(@ApiParam(name = "teamId", value = "队伍的id", required = true)
                              @RequestBody Map<String,Long> object) throws FullTeamException {


        Long teamId= (Long) object.get("teamId");
       if (joinTeamService.isFull(teamId)){
           throw new FullTeamException("队伍已满员");
       }
        return R.ok("已申请",joinTeamService.applyJoin(teamId,StpUtil.getLoginIdAsLong()));
    }


    @ApiOperation("查看全部申请")
    @SaCheckLogin//
    @PostMapping("findAllApply")
    public RES findAllApply(@ApiParam(name = "teamId", value = "队伍的id", required = true)
                              @RequestBody Map<String,Long> object)  {
        Long teamId=object.get("teamId");
        return R.ok("已申请",joinTeamService.findAllApply(teamId));
    }

    @ApiOperation("绑定项目")
    @SaCheckLogin//
    @PostMapping("bindProJ")
    public RES bindProJ(@RequestBody BindProjTeamDto object)  {
        Long teamId= object.getTeamId();
        Long projectId = object.getProjectId();

        if (teamProjectService.teamBindProject(teamId,projectId)){
            return R.ok("绑定完成");
        }else {
            teamRepository.deleteById(teamId);
            throw new RuntimeException("绑定失败");
        }

    }

    @ApiOperation("查询队伍")
    @SaIgnore//
    @PostMapping("findAllTeam")
    public RES findAllTeam(@RequestBody FindTeamDto dto)  {
        return R.ok(teamService.findTeam(dto));
    }

    @ApiOperation("获取本用户创建的队伍")
    @SaCheckLogin//
    @GetMapping("getMyTeam")
    public RES getMyTeam()  {
        return R.ok(teamService.findMyTeam(StpUtil.getLoginIdAsLong()));
    }

    @ApiOperation("获取本用户加入的全部队伍")
    @SaCheckLogin//
    @GetMapping("getAllMyTeam")
    public RES getAllMyTeam()  {
        return R.ok(teamService.findAllMyTeam(StpUtil.getLoginIdAsLong()));
    }

    @ApiOperation("获取本用户全部申请")
    @SaCheckLogin//
    @GetMapping("getAllMyApply")
    public RES getAllMyApply()  {
        return R.ok(joinTeamService.findAllMyApply(StpUtil.getLoginIdAsLong()));
    }

    @ApiOperation("根据id查询队伍信息")
    @SaCheckLogin//
    @PostMapping("findTeamById")
    public RES findTeamById(@ApiParam(name = "teamId", value = "队伍的id", required = true)
                            @RequestBody Map<String,Long> object)  {
        Long teamId=object.get("teamId");
        return R.ok("已申请",teamService.findTeamById(teamId));
    }




}
